package cn.lanqiao.blog.service.impl;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.lanqiao.blog.dao.ArticleMapper;
import cn.lanqiao.blog.dao.CheckMapper;
import cn.lanqiao.blog.dao.LableMapper;
import cn.lanqiao.blog.dao.UserMapper;
import cn.lanqiao.blog.entity.Article;
import cn.lanqiao.blog.entity.User;
import cn.lanqiao.blog.service.ArticleServicle;
import cn.lanqiao.blog.vo.ArticleVO;

@Component
public class ArticleServiceImpl implements ArticleServicle{

	@Autowired
	private ArticleMapper articleMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private CheckMapper checkMapper;
	
	@Autowired
	private LableMapper lableMapper;
	
	@Override
	public PageInfo<ArticleVO> getAllVisible(Integer userId, Integer pageNum, Integer pageSize, String title, String username, Integer lableId) {
		// TODO Auto-generated method stub
		List<ArticleVO> list = articleMapper.getAllVisible(userId, title, username, lableId);
		List<ArticleVO> list2 = new ArrayList<>();
		if(pageNum * pageSize >= list.size()) {
			for(int i = (pageNum - 1) * pageSize; i < list.size(); i++) {
				list2.add(list.get(i));
			}
		}else {
			for(int i = (pageNum - 1) * pageSize; i < pageNum * pageSize; i++) {
				list2.add(list.get(i));
			}
		}
		PageInfo<ArticleVO> page = new PageInfo<>(list2);
		return page;
	}

	@Override
	public PageInfo<ArticleVO> getFollow(Integer userId, Integer pageNum, Integer pageSize, String title, String username, Integer lableId) {
		// TODO Auto-generated method stub
		List<ArticleVO> list = articleMapper.getFollow(userId, title, username, lableId);
		List<ArticleVO> list2 = new ArrayList<>();
		if(pageNum * pageSize >= list.size()) {
			for(int i = (pageNum - 1) * pageSize; i < list.size(); i++) {
				list2.add(list.get(i));
			}
		}else {
			for(int i = (pageNum - 1) * pageSize; i < pageNum * pageSize; i++) {
				list2.add(list.get(i));
			}
		}
		PageInfo<ArticleVO> page = new PageInfo<>(list2);
		return page;
	}

	@Override
	public PageInfo<ArticleVO> getMyArticle(Integer userId, Integer pageNum, Integer pageSize, String title,Integer lableId) {
		// TODO Auto-generated method stub
		List<ArticleVO> list = articleMapper.getMyArticle(userId, title, lableId);
		List<ArticleVO> list2 = new ArrayList<>();
		if(pageNum * pageSize >= list.size()) {
			for(int i = (pageNum - 1) * pageSize; i < list.size(); i++) {
				list2.add(list.get(i));
			}
		}else {
			for(int i = (pageNum - 1) * pageSize; i < pageNum * pageSize; i++) {
				list2.add(list.get(i));
			}
		}
		PageInfo<ArticleVO> page = new PageInfo<>(list2);
		return page;
	}

	@Override
	public int getArticleCommentCount(Integer articleId) {
		// TODO Auto-generated method stub
		return articleMapper.getArticleCommentCount(articleId);
	}

	@Override
	public int increasePraise(Integer articleId) {
		// TODO Auto-generated method stub
		return articleMapper.increasePraise(articleId);
	}

	@Override
	public int increaseViews(Integer articleId) {
		// TODO Auto-generated method stub
		return articleMapper.increaseViews(articleId);
	}

	@Override
	public PageInfo<ArticleVO> getCollectionList(Integer userId, Integer pageNum, Integer pageSize, String title, String username, Integer lableId) {
		// TODO Auto-generated method stub
		List<ArticleVO> list = articleMapper.getCollectionList(userId, title, username, lableId);
		List<ArticleVO> list2 = new ArrayList<>();
		if(pageNum * pageSize >= list.size()) {
			for(int i = (pageNum - 1) * pageSize; i < list.size(); i++) {
				list2.add(list.get(i));
			}
		}else {
			for(int i = (pageNum - 1) * pageSize; i < pageNum * pageSize; i++) {
				list2.add(list.get(i));
			}
		}
		PageInfo<ArticleVO> page = new PageInfo<>(list2);
		return page;
	}

	@Override
	public PageInfo<ArticleVO> showAdminArticle(Integer adminId, Integer viewerId, Integer pageNum, Integer pageSize, String title, Integer lableId) {
		// TODO Auto-generated method stub
		List<ArticleVO> list = null;
		List<ArticleVO> list2 = new ArrayList<>();
		if(userMapper.isAdminFans(adminId, viewerId) > 0) {
			list = articleMapper.getAdminFansVisibleArticle(adminId, title, lableId);
			if(pageNum * pageSize >= list.size()) {
				for(int i = (pageNum - 1) * pageSize; i < list.size(); i++) {
					list2.add(list.get(i));
				}
			}else {
				for(int i = (pageNum - 1) * pageSize; i < pageNum * pageSize; i++) {
					list2.add(list.get(i));
				}
			}
		}else {
			list = articleMapper.getNotAdminFansVisibleArticle(adminId, title, lableId);
			if(pageNum * pageSize >= list.size()) {
				for(int i = (pageNum - 1) * pageSize; i < list.size(); i++) {
					list2.add(list.get(i));
				}
			}else {
				for(int i = (pageNum - 1) * pageSize; i < pageNum * pageSize; i++) {
					list2.add(list.get(i));
				}
			}
		}
		PageInfo<ArticleVO> page = new PageInfo<>(list2);
		return page;
	}

	@Override
	public int addCollection(Integer userId, Integer articleId) {
		// TODO Auto-generated method stub
		return articleMapper.addCollection(userId, articleId);
	}

	@Override
	public int deleteCollection(Integer userId, Integer articleId) {
		// TODO Auto-generated method stub
		return articleMapper.deleteCollection(userId, articleId);
	}

	@Override
	public int changeVisible(Integer articleId, Integer visible) {
		// TODO Auto-generated method stub
		return articleMapper.changeVisible(articleId, visible);
	}

	@Override
	public int changeCommentable(Integer articleId, Integer commentable) {
		// TODO Auto-generated method stub
		return articleMapper.changeCommentable(articleId, commentable);
	}

	@Override
	public PageInfo<ArticleVO> getTouristVisible(Integer pageNum, Integer pageSize, String title, String username, Integer lableId) {
		// TODO Auto-generated method stub
		List<ArticleVO> list = articleMapper.getTouristVisible(title, username, lableId);
		List<ArticleVO> list2 = new ArrayList<>();
		if(pageNum * pageSize >= list.size()) {
			for(int i = (pageNum - 1) * pageSize; i < list.size(); i++) {
				list2.add(list.get(i));
			}
		}else {
			for(int i = (pageNum - 1) * pageSize; i < pageNum * pageSize; i++) {
				list2.add(list.get(i));
			}
		}
		PageInfo<ArticleVO> page = new PageInfo<>(list2);
		return page;
	}

	@Override
	public int getTouristTotal(String title, String username, Integer lableId) {
		// TODO Auto-generated method stub
		return articleMapper.getTouristVisible(title, username, lableId).size();
	}

	@Override
	public int getAllVisibleTotal(Integer userId, String title, String username, Integer lableId) {
		// TODO Auto-generated method stub
		return articleMapper.getAllVisible(userId, title, username, lableId).size();
	}

	@Override
	public int getMyFollowTotal(Integer userId, String title, String username, Integer lableId) {
		// TODO Auto-generated method stub
		return articleMapper.getFollow(userId, title, username, lableId).size();
	}

	@Override
	public int getMyCollectionTotal(Integer userId, String title, String username, Integer lableId) {
		// TODO Auto-generated method stub
		return articleMapper.getCollectionList(userId, title, username, lableId).size();
	}

	@Override
	public int getAdminFansTotal(Integer adminId, String title, Integer lableId) {
		// TODO Auto-generated method stub
		return articleMapper.getAdminFansVisibleArticle(adminId, title, lableId).size();
	}

	@Override
	public int getNotAdminFansTotal(Integer adminId, String title, Integer lableId) {
		// TODO Auto-generated method stub
		return articleMapper.getNotAdminFansVisibleArticle(adminId, title, lableId).size();
	}

	public boolean isAdminFans(Integer adminId, Integer viewerId) {
		if(userMapper.isAdminFans(adminId, viewerId) > 0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public int getMyArticleTotal(Integer userId, String title, Integer lableId) {
		// TODO Auto-generated method stub
		return articleMapper.getMyArticle(userId, title, lableId).size();
	}

	@Override
	public int writeArticle(Article article, Integer[] lableIds) {
		// TODO Auto-generated method stub
		article.setViews(0);
		article.setCommentCount(0);
		article.setPraise(0);
		if(checkMapper.getCheck() == 1) {
			article.setIsCheck(1);
			articleMapper.writeArticle(article);
			int articleId = article.getId();
			List<User> fansList = userMapper.getFansList(article.getUserId());
			for(User fans: fansList) {
				articleMapper.addFansArticle(fans.getId(), articleId);
				articleMapper.addFansCommentable(fans.getId(), articleId);
			}
			for(Integer lableId: lableIds) {
				lableMapper.reArticleLable(articleId, lableId);
			}
			return 1;
		}else {
			article.setIsCheck(0);
			articleMapper.writeArticle(article);
			int articleId = article.getId();
			List<User> fansList = userMapper.getFansList(article.getUserId());
			for(User fans: fansList) {
				articleMapper.addFansArticle(fans.getId(), articleId);
				articleMapper.addFansCommentable(fans.getId(), articleId);
			}
			for(Integer lableId: lableIds) {
				lableMapper.reArticleLable(articleId, lableId);
			}
			return 0;
		}
		
	}

	@Override
	public ArticleVO getArticleDetail(Integer articleId) {
		// TODO Auto-generated method stub
		return articleMapper.getArticleDetail(articleId);
	}

	@Override
	public int praiseArticle(Integer userId, Integer articleId) {
		// TODO Auto-generated method stub
		return articleMapper.praiseArticle(userId, articleId);
	}

	@Override
	public int delPraise(Integer userId, Integer articleId) {
		// TODO Auto-generated method stub
		return articleMapper.delPraise(userId, articleId);
	}

	@Override
	public int decreasePraise(Integer articleId) {
		// TODO Auto-generated method stub
		return articleMapper.decreasePraise(articleId);
	}

	@Override
	public int editArticle(Article article, Integer[] lableIds) {
		// TODO Auto-generated method stub
		int result1 = 0;
		int result2 = 0;
		int result3 = 0;
		result1 = articleMapper.editArticle(article);
		result2 = lableMapper.deleteArticleLable(article.getId());
		for(Integer lableId: lableIds) {
			result3 += lableMapper.reArticleLable(article.getId(), lableId);
		}
		if(result1 > 0 && result2 > 0 && result3 == lableIds.length) {
			return 1;
		}else {
			return 0;
		}
	}

	@Override
	public PageInfo<ArticleVO> getArticlePage(Integer pageNum, Integer pageSize,Article record) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		List<ArticleVO> list = articleMapper.getAllArticle(record);
		PageInfo<ArticleVO> page = new PageInfo<>(list);
		return page;
	}
	
	@Override
	public int deleteArticle(Integer id) {
		// TODO Auto-generated method stub
		
		articleMapper.delete(id);
		int result = articleMapper.deleteByPrimaryKey1(id);
		if(result == 1) {
			return 1;
		}
		return 0;
	}
	//审核
	@Override
	public int updateArticle(Article article) {
		// TODO Auto-generated method stub
		return articleMapper.check(article.getId());
	}

	@Override
	public PageInfo<ArticleVO> getNoArticlePage(Integer pageNum, Integer pageSize,Article record) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize);
		PageInfo<ArticleVO> page = new PageInfo<>(articleMapper.getAllNoArticle(record));
		return page;
	}

}
